package com.example.myapplication2;

import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.myapplication2.Adapter.ViewPagerDemo;

import java.util.ArrayList;
import java.util.HashMap;

public class ViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    public static final String[] array = {"Q", "A", "B"};
    public static final int[] ioc = {R.color.black, R.color.teal_700, R.color.purple_200};
    private ArrayList<HashMap<String, Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        ViewPager viewPager = findViewById(R.id.ViewPager);
        list = new ArrayList<>();

        for (int i = 0; i < array.length; i++) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("ioc", ioc[i]);
            hashMap.put("text", array[i]);
            list.add(hashMap);
        }

        ViewPagerDemo adapter = new ViewPagerDemo(this, list);
        viewPager.setAdapter(adapter);
        //设置监听并使其实现
        viewPager.addOnPageChangeListener(this);
    }

    //翻页偏移量,position当前翻页索引,positionOffset偏移百分比,positionOffsetPixels像素便宜距离
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        System.out.println(position);
        System.out.println(positionOffset);
        System.out.println(positionOffsetPixels);
    }
    //翻页结束最终页面索引
    @Override
    public void onPageSelected(int position) {
        HashMap<String, Object> hashMap = list.get(position);
        int o = (int) hashMap.get("ioc");
        String text = (String) hashMap.get("text");
        Toast.makeText(this,"选中"+o+":"+text,Toast.LENGTH_SHORT).show();
    }
    //翻页状态,0静止,1开始,2结束
    @Override
    public void onPageScrollStateChanged(int state) {
        System.out.println(state);
    }
}